package com.sport.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.sport.common.core.domain.entity.SysUser;
import com.sport.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sport.common.annotation.Log;
import com.sport.common.core.controller.BaseController;
import com.sport.common.core.domain.AjaxResult;
import com.sport.common.enums.BusinessType;
import com.sport.system.domain.SportItem;
import com.sport.system.service.ISportItemService;
import com.sport.common.utils.poi.ExcelUtil;
import com.sport.common.core.page.TableDataInfo;

/**
 * 比赛项目Controller
 *
 * @author wyd
 * @date 2022-04-07
 */
@RestController
@RequestMapping("/system/item")
public class SportItemController extends BaseController
{
    @Autowired
    private ISportItemService sportItemService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询比赛项目列表
     */
//    @PreAuthorize("@ss.hasPermi('system:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(SportItem sportItem)
    {
        startPage();
        List<SportItem> list = sportItemService.selectSportItemList(sportItem);
        ArrayList<Object> objects = new ArrayList<>();
        for (SportItem item : list) {
            SysUser sysUser = sysUserService.selectUserByUserName(item.getItemJudgementid());
            item.setItemJudgementname(sysUser.getNickName());
            objects.add(item);
        }
        return getDataTable(objects);
    }

    /**
     * 导出比赛项目列表
     */
//    @PreAuthorize("@ss.hasPermi('system:item:export')")
    @Log(title = "比赛项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SportItem sportItem)
    {
        List<SportItem> list = sportItemService.selectSportItemList(sportItem);
        ExcelUtil<SportItem> util = new ExcelUtil<SportItem>(SportItem.class);
        util.exportExcel(response, list, "比赛项目数据");
    }

    /**
     * 获取比赛项目详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:item:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(sportItemService.selectSportItemById(id));
    }

    /**
     * 新增比赛项目
     */
//    @PreAuthorize("@ss.hasPermi('system:item:add')")
    @Log(title = "比赛项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SportItem sportItem)
    {
        sportItem.setId(UUID.randomUUID().toString().replace("-",""));
        SportItem sportItem1 = new SportItem();
        sportItem1.setItemName(sportItem.getItemName());
        List<SportItem> sportItems = sportItemService.selectSportItemList(sportItem1);
        for (SportItem item : sportItems) {
            if(item.equals(sportItem)) {
                return AjaxResult.error("比赛项目已安排，请勿重复安排！");
            }
        }

        return toAjax(sportItemService.insertSportItem(sportItem));
    }

    /**
     * 修改比赛项目
     */
//    @PreAuthorize("@ss.hasPermi('system:item:edit')")
    @Log(title = "比赛项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SportItem sportItem)
    {
        return toAjax(sportItemService.updateSportItem(sportItem));
    }

    /**
     * 删除比赛项目
     */
//    @PreAuthorize("@ss.hasPermi('system:item:remove')")
    @Log(title = "比赛项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sportItemService.deleteSportItemByIds(ids));
    }
}
